package com.zhao.service.impl;

import com.zhao.service.MenuService;
import com.zhao.util.HttpRequest;
import com.zhao.util.StringUtil;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.zhao.base.AccessTokenBase;
import com.zhao.base.TokenThread;

import net.sf.json.JSONObject;

public class MenuServiceImpl implements MenuService {
	Logger log=LoggerFactory.getLogger(MenuServiceImpl.class);
	private String createUrl="https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN";  
	private String deleteUrl="https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=ACCESS_TOKEN";  
	private String createChangeUrl="https://api.weixin.qq.com/cgi-bin/menu/addconditional?access_token=ACCESS_TOKEN";
	private String deleteChangeUrl="https://api.weixin.qq.com/cgi-bin/menu/delconditional?access_token=ACCESS_TOKEN";
	private String selectMenuUrl="https://api.weixin.qq.com/cgi-bin/menu/get?access_token=ACCESS_TOKEN";
	private String accessToken="";
	
	@Override
	public void createMenu(String menuJson){
		try {
			accessToken=AccessTokenBase.getAccessTokenStr();
    		createUrl=createUrl.replace("ACCESS_TOKEN",accessToken);
			deleteMenu(accessToken);
			JSONObject json=HttpRequest.httpsRequest(createUrl,"POST", menuJson);
			String errcode=json.getString("errcode");
			if(errcode.equals("0")) {
				log.info("菜单创建成功！");
			}else {
				log.info("菜单创建失败："+json.toString());
			}
		} catch (Exception e) {
			log.info("菜单创建异常！"+e.getMessage());
		}
	}

	@Override
	public void deleteMenu(String accessToken) {
		accessToken=AccessTokenBase.getAccessTokenStr();
		deleteUrl=deleteUrl.replace("ACCESS_TOKEN", accessToken);
		JSONObject json=HttpRequest.httpsRequest(deleteUrl,"GET",null);
		log.info("删除菜单成功"+json.toString());
	}
	
	
	@Override
	public void createChangeMenu(String menuJson) {
		try {
			accessToken=AccessTokenBase.getAccessTokenStr();
			createChangeUrl=createChangeUrl.replace("ACCESS_TOKEN",accessToken);
			JSONObject result=HttpRequest.httpsRequest(createChangeUrl,"POST", menuJson);
			String menuid=result.getString("menuid");
			if(!StringUtil.isEmpty(menuid)) {
				log.info("菜单创建成功,menuid:"+menuid);
			}else {
				log.info("菜单创建失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.info("菜单创建异常！"+e.getMessage());
		}
	}

	@Override
	public void deleteChangeMenu(String menuidStr) {
		try {
			accessToken=AccessTokenBase.getAccessTokenStr();
			deleteChangeUrl=deleteChangeUrl.replace("ACCESS_TOKEN",accessToken);
			JSONObject result=HttpRequest.httpsRequest(deleteChangeUrl, "POST", menuidStr);
			System.out.println(result.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public JSONObject selectMenu() {
		try {
			accessToken=AccessTokenBase.getAccessTokenStr();
			selectMenuUrl=selectMenuUrl.replace("ACCESS_TOKEN",accessToken);
			JSONObject result=HttpRequest.httpsRequest(selectMenuUrl, "GET", null);
			log.info("查询菜单结果"+result);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
}
